package cn.hiapi.shopkeeper.service.activity;

import cn.hiapi.basic.ResUtils;
import cn.hiapi.basic.entity.BasicEntity;
import cn.hiapi.basic.exception.BasicException;
import cn.hiapi.basic.redis.service.RedisService;
import cn.hiapi.basic.token.Token;
import cn.hiapi.interfaces.DispatchContext;
import cn.hiapi.shopkeeper.consts.AppConst;
import cn.hiapi.shopkeeper.entity.activity.Activity;
import cn.hiapi.shopkeeper.entity.activity.ActivityLottery;
import cn.hiapi.shopkeeper.entity.activity.ActivityLotteryItem;
import cn.hiapi.shopkeeper.entity.order.ActivityOrder;
import cn.hiapi.shopkeeper.enums.ActivityType;
import cn.hiapi.shopkeeper.jpa.activity.ActivityLotteryJpa;
import cn.hiapi.shopkeeper.request.CreateOrderRequest;
import cn.hiapi.utils.RandomUtils;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author AdinZ
 * @date 2022/12/13 0:35
 */
@Service
public class ActivityLotteryService extends AbsActivityService<ActivityLotteryJpa, ActivityLottery, Long> {


    public ActivityLotteryService(DispatchContext dispatchContext) {
        super(dispatchContext);
    }

    @Override
    public ActivityType getActivityType() {
        return ActivityType.LOTTERY;
    }

    @Override
    @Transactional
    public BasicEntity saveExpand(long activityId, JSONObject data) {
        ActivityLottery lottery = data.toJavaObject(ActivityLottery.class);
        lottery.setAid(activityId);
        if (lottery.getDayCount() <= 0)
            throw new BasicException("请填写 每人每天抽奖次数");
        if (lottery.getDayLottery() <= 0)
            throw new BasicException("请填写 每人每天中奖次数");
        if (lottery.getRate() == null || lottery.getRate().compareTo(BigDecimal.ZERO) < 0) {
            throw new BasicException("请填写 中奖概率");
        }

        if (!data.containsKey("items"))
            throw new BasicException("请设置奖品信息");
        List<ActivityLotteryItem> items = data.getJSONArray("items").toJavaList(ActivityLotteryItem.class);
        if (items.isEmpty()) throw new BasicException("没有设置奖品信息");
        if (items.size() > 7) throw new BasicException("奖品数量不能超过7个");


        this.save(lottery);

        items.forEach(e -> {
            e.setId(null);
            e.setLid(lottery.getId());
            e.setAid(activityId);
            if (e.getItemId() <= 0) {
                e.setItemId(RandomUtils.randomSnowflake());
            }
            if (StringUtils.isEmpty(e.getTitle()))
                throw new BasicException("奖品名称填写完整");
            if (StringUtils.isEmpty(e.getThumb()))
                throw new BasicException("商品图片没有上传");
            if (e.getRate() == null || e.getRate().compareTo(BigDecimal.ZERO) < 0)
                throw new BasicException("奖品中奖概率没有填写完成");
            if (e.getStockNum() <= 0) throw new BasicException("奖品数量不完整");
            if (e.getPrice() == null || e.getPrice().compareTo(BigDecimal.ZERO) <= 0)
                throw new BasicException("商品价格没填写完整");
        });
        this.dispatchContext.getServiceOne(ActivityLotteryItemService.class).save(items);
        return lottery;
    }


    @Override
    public JSONObject getExpand(long aid) {
        JSONObject expand = super.getExpand(aid);
        if(expand==null)return null;
        expand.put("items", this.dispatchContext.getServiceOne(ActivityLotteryItemService.class).getByAidRandom(aid));
        return expand;
    }

    @Override
    @Transactional
    public int deleteByAid(long aid) {
        Integer integer = this.repository.deleteByAid(aid);
        this.dispatchContext.getServiceOne(ActivityLotteryItemService.class).deleteByAid(aid);
        return ResUtils.getDefault(integer);
    }

    @Override
    public ActivityOrder createOrder(BasicEntity basic, Activity activity, Map<String, Long> incrementStock, CreateOrderRequest request, Token<Object> token) {
        throw new BasicException("分享即可抽奖");
    }




}
